<?php
declare (strict_types=1);

namespace app\admin\listener;

use think\Exception;

class Logs
{
    public function handle($logs)
    {
        try {
            if (!$logs['tableUniqueTitle'] && $logs['writeLog'] != 2) return true;
            $model = $logs['model'];
            if ($logs['writeLog'] == 2) {
                if (method_exists($model, 'writeLog')) {
                    return $model::writeLog($logs);
                }
                throw new Exception('请在主Model定义writeLog自定义日志方法');
            }
            $msg = '';
            if (in_array($logs['operation'], ['save'])) {
                $original = $logs['original'];
                $operation = $logs['tableUniqueTitle'];
                $editField = $logs['editField'];
                foreach ($editField as $item) {
                    $field = $item['field'];
                    if ($model->$field != $original->$field && $item['mode'] >= 2) {
                        switch ($item['type']) {
                            case 'ratio':
                                $original->$field = $original->$field * 100 . '%';
                                $model->$field = $model->$field * 100 . '%';
                                break;
                            case 'img':
                                $original->$field = '<a href="' . $original->$field . '" target="_blank">' . $original->$field . '</a>';
                                $model->$field = '<a href="' . $model->$field . '" target="_blank">' . $model->$field . '</a>';
                                break;
                            case "editor":
                                $original->$field = '<a href="javascript:;" class="openEditor" data-value="' . $original->$field . '">点击预览</a>';
                                $model->$field = '<a href="javascript:;" class="openEditor" data-value="' . $model->$field . '">点击预览</a>';
                                break;
                            case "select":
                            case "radio":
                                $original->$field = isset($item['value'][$original->$field]) ? $item['value'][$original->$field] : '无';
                                $model->$field = isset($item['value'][$model->$field]) ? $item['value'][$model->$field] : '无';
                                break;
                            case "checkbox":

                                break;
                        }

                        $msg .= '，《' . $item['title'] . '由【' . $original->$field . '】更改为【' . $model->$field . '】》';
                    }
                }
                $writeLogKey = explode(',', $logs['writeLogKey']['key']);
                $writeLogCVal = explode(',', $logs['writeLogKey']['val']);
                foreach ($writeLogKey as $key => $item) {
                    if (isset($original->$item)) {
                        $msg .= $writeLogCVal[$key] . '【' . $original->$item . '】';
                    }
                }
                if ($msg) {
                    \app\common\model\Logs::adds($operation . $msg . ' -- 标识ID【' . $model->id . '】', $logs['admin']->id, $logs['admin']->admin_name);
                }
            } else {
                $operation = $this->getOperation($logs['operation']);
                switch ($logs['operation']) {
                    case "updateAll":
                        $msg = '【' . $logs['filedTitle']['title'] . '】改为【' . $logs['valTitle'] . '】' . '标识ID【' . implode(',', $model->ids) . '】';
                        break;
                    case "deleteAll":
                        $msg = '标识ID【' . implode(',', $model->ids) . '】';
                        break;
                    case "update":
                    case "delete":
                        if ($logs['operation'] == 'update') {
                            $msg = '【' . $logs['filedTitle']['title'] . '】改为【' . $logs['valTitle'] . '】' . '标识ID【' . $model->id . '】';
                        } else {
                            $msg = '标识ID【' . $model->id . '】';
                        }
                        $writeLogKey = explode(',', $logs['writeLogKey']['key']);
                        $writeLogCVal = explode(',', $logs['writeLogKey']['val']);
                        foreach ($writeLogKey as $key => $item) {
                            if (isset($model->$item)) {
                                $msg .= $writeLogCVal[$key] . '【' . $model->$item . '】';
                            }
                        }
                        break;
                }
                \app\common\model\Logs::adds($operation . '《' . $logs['tableUniqueTitle'] . '》' . $msg, $logs['admin']->id, $logs['admin']->admin_name);
            }
            return true;
        } catch (\Exception $e) {
            throw new Exception('记录日志发生错误了：' . $e->getMessage());
        }

    }

    public function getOperation($name)
    {
        switch ($name) {
            case "save":
                return '修改';
            case "create":
                return '添加';
            case "update":
                return '更新状态';
            case "updateAll":
                return '批量更新状态';
            case "delete":
                return '删除';
            case "deleteAll":
                return '批量删除';
            default:
                return '操作';
        }
    }
}